package cn.codesea.housework_user.ui.worker;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.ui.BaseActivity;
import cn.codesea.housework_user.ui.LoginActivity;
import cn.codesea.housework_user.utils.SPUtil;

public class MainWorkerActivity extends BaseActivity {

    @BindView(R.id.navigation_bar)
    BottomNavigationBar bottomNavigationBar;

    Fragment tabFragment1;
    Fragment tabFragment2;

    private int lastSelectedPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_worker);
        ButterKnife.bind(this);
        init();
    }

    public void init(){
        initView();// 初始化界面
    }

    private void initView(){

        // 初始化 fragment，即内嵌的三个页面
        tabFragment1 = new Tab1WorkManageFragment();
        tabFragment2 = new Tab2MineFragment();

        // 默认第一个界面
        getSupportFragmentManager().beginTransaction().replace(R.id.container, tabFragment1).commitAllowingStateLoss();


        // 底部导航栏，添加项
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.ic_home, "工作管理").setActiveColorResource(R.color.primary))
                .addItem(new BottomNavigationItem(R.drawable.ic_mine, "个人中心").setActiveColorResource(R.color.primary))
                .setFirstSelectedPosition(lastSelectedPosition)
                .initialise();

        // 添加事件
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
            @Override
            public void onTabSelected(int position) {
                setScrollableText(position);// 点击事件
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

    }

    /**
     * 底部导航栏点击事件处理
     * @param position
     */
    private void setScrollableText(int position) {
        switch (position) {
            case 0:
                getSupportFragmentManager().beginTransaction().replace(R.id.container, tabFragment1).commitAllowingStateLoss();
                break;
            case 1:

                /* */
                // 点击 “ic_mine”，首先判断用户是否登录
                if(SPUtil.isLogin(MainWorkerActivity.this)){
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, tabFragment2).commitAllowingStateLoss();
                }else {
                    bottomNavigationBar.selectTab(0);
                    // 未登录，跳转登录页
                    Intent intent = new Intent();
                    intent.setClass(MainWorkerActivity.this,LoginActivity.class);
                    startActivity(intent);
                }

                break;
            default:
                getSupportFragmentManager().beginTransaction().replace(R.id.container, tabFragment1).commitAllowingStateLoss();
                break;
        }
    }

}
